import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

/**
 * If there are no bugs, it was created by Chen FengYao on 2018/5/2;
 * Otherwise, I don't know who created it either
 */

public class Main {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager
                    .getConnection("jdbc:mysql://localhost:3306/lanou3",
                            "root", "123456");
            // 开启事务
            // 把autoCommit关闭,相当于 使用了 begin语句
            conn.setAutoCommit(false);
            String sql1 = "UPDATE bank " +
                    "SET money = money - 5 " +
                    "WHERE cname = '鸣人'";

            String sql2 = "UPDATE bank " +
                    "SET money = money + 5 " +
                    "WHERE cname = '佐助'";

            Statement statement = conn.createStatement();
            statement.executeUpdate(sql1);// 执行sql1
            statement.executeUpdate(sql2);// 执行sql2

            conn.commit();// 提交事务
            // 关闭资源
            statement.close();
            conn.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
